package com.bonana.bozone.controller;

import com.alibaba.druid.filter.AutoLoad;
import com.alibaba.fastjson.JSON;
import com.bonana.core.entity.CoreUserProfile;
import com.bonana.core.entity.mybatis.*;
import com.bonana.core.result.Response;
import com.bonana.core.result.ResponseCode;
import com.bonana.core.services.BannerService;
import com.bonana.core.services.BoBoService;
import com.bonana.core.services.ProjectService;
import com.bonana.core.vo.OperStatusVO;
import com.bonana.core.vo.ProjectCommentVo;
import com.bonana.core.vo.ProjectVoteVo;
import com.bonana.core.vo.UserCardVO;
import com.bonana.utils.ConvertUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.ws.rs.Path;
import java.util.List;

/**
 * Created by Administrator on 2016/7/6.
 */
@Controller
@RequestMapping(value="project")
public class ProjectControler {
    @Autowired
    ProjectService projectService;
    @Autowired
    BoBoService boBoService;
    @Autowired
    HttpServletRequest request;
    @Autowired
    HttpSession session;
    @Autowired
    BannerService bannerService;

    private int getCurUid() {
        int uid = 0;
        if (session.getAttribute("session.uid") != null) {
            uid = ConvertUtils.toInteger(session.getAttribute("session.uid"));
        }
        return uid;
    }

    @RequestMapping(value = "{pid}", method= RequestMethod.GET)
    public String ProjectHome (@PathVariable int pid, Model model){
        Project proj = projectService.getProjectById(pid);
        model.addAttribute("proj", proj);
        projectService.IncProjectViewCnt(pid, getCurUid());

        CoreUserProfile sender = boBoService.queryUserProfileByUid(proj.getProjectSender(), (long) 0);
        model.addAttribute("sender", sender);

        List<VoteLog> projVote = projectService.getProjectVote(pid);
        model.addAttribute("votecnt", projVote.size());

        List<ProjectActive> projActives = projectService.getProjectActiveByPId(pid);
        model.addAttribute("projActives", projActives);

        List<CoreUserProfile> projVisitor = projectService.getProjectRecentVisitor(pid, 6);
        model.addAttribute("projVisitor", projVisitor);

        List<Project> hotProjects = projectService.getHotProjects(6);
        model.addAttribute("hotProjects", hotProjects);

        OperStatusVO status = projectService.getRelation(pid, getCurUid());
        model.addAttribute("op", status);

        return "project/card";
    }

    @RequestMapping(value ="")
    public String ProjectList(Model model) {
        List<Banner> adBanner = bannerService.getAdBanner();
        model.addAttribute("adBanner", adBanner);
        return "project/list";
    }

    @RequestMapping(value="getProject", method = RequestMethod.POST)
    @ResponseBody
    public PageInfo getBanner(Integer pgNum, Integer pgSize, @RequestParam(required = false) String query) {
        List<Project> list = projectService.getProjs(pgNum, pgSize, query);
        return new PageInfo(list);
    }

    @RequestMapping(value = "getProjComments")
    @ResponseBody
    public PageInfo getProjComments(Integer projectId, Integer pgNum, Integer pgSize) {
        List<ProjectCommentVo> comments = projectService.getProjectCommentsbyPid(projectId, pgNum, pgSize);
        return new PageInfo(comments);
    }

    @RequestMapping(value = "getProjVotes")
    @ResponseBody
    public PageInfo getProjVotes(Integer projectId, Integer pgNum, Integer pgSize) {
        List<ProjectVoteVo> votes = projectService.getProjectVotesByPid(projectId, pgNum, pgSize);
        return new PageInfo(votes);
    }

    @RequestMapping(value = "doFav")
    @ResponseBody
    public Response doFav(Integer projectId) {
        int uid = getCurUid();
        if (uid > 0) {
            projectService.FavProject(projectId, uid);
            return new Response(ResponseCode.SUCC);
        } else {
            return new Response(ResponseCode.USER_NOT_LOGIN);
        }
    }

    @RequestMapping(value = "addComment")
    @ResponseBody
    public Response addComment(Integer projectId, Integer parrentId, String content) {
        int uid = getCurUid();
        if (uid > 0) {
            projectService.addComment(projectId, uid, parrentId, content);
            return new Response(ResponseCode.SUCC);
        } else {
            return new Response(ResponseCode.USER_NOT_LOGIN);
        }
    }

    @RequestMapping(value = "voteProj")
    @ResponseBody
    public Response voteProj(Integer projectId) {
        int uid = getCurUid();
        if (uid > 0) {
            return projectService.voteForProject(projectId, uid, request.getRemoteAddr());
        } else {
            return new Response(ResponseCode.USER_NOT_LOGIN);
        }
    }
}
